package com.yourwork.basedao.imp;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.yourwork.basedao.BaseDao;

public class BaseDaoImp implements BaseDao  {
	@Autowired//@Autowired注解的作用是进行自动注入的标识
	@Qualifier(value="sessionFactory")//@Qualifier注解的作用是配置注入的对象来源
	private SessionFactory seFa;//SessionFactory变量seFa的作用是Session工厂，负责生产Session对象
	//getSession方法的作用是生产Session，Session是Hibernate与数据库交互的工具
	private Session getSession(){
		//声明式事务不需要手动给开启Session
		return seFa.openSession();
		//声明式事务只需向SessionFactory索要Session
		//return seFa.getCurrentSession();
	}
	
	public void save(Object obj) {
		// TODO Auto-generated method stub
		Session s=getSession();
		//Transaction tr=s.beginTransaction();//Transaction的作用是开启事务
		s.save(obj);
		//tr.commit();
		//声明式事务不需要手动给关闭Session
		//s.close();

	}

	public void saveOrUpdate(Object obj) {
		// TODO Auto-generated method stub
		Session s=getSession();
		//Transaction tr=s.beginTransaction();//Transaction的作用是开启事务
		s.saveOrUpdate(obj);
		//tr.commit();
		//声明式事务不需要手动给关闭Session
		//s.close();
	}

	public void update(Object obj) {
		// TODO Auto-generated method stub
		Session s=getSession();
		//Transaction tr=s.beginTransaction();//Transaction的作用是开启事务
		s.update(obj);
		//tr.commit();
		//声明式事务不需要手动给关闭Session
		//s.close();
	}

	public void delete(Object obj) {
		// TODO Auto-generated method stub
		Session s=getSession();
		//Transaction tr=s.beginTransaction();//Transaction的作用是开启事务
		s.delete(obj);
		//tr.commit();
		//声明式事务不需要手动给关闭Session
		//s.close();
	}

	public void deleteById(Class clazz, Serializable id) {
		// TODO Auto-generated method stub
		Object obj=getById(clazz, id);
		Session s=getSession();
		//Transaction tr=s.beginTransaction();//Transaction的作用是开启事务
		s.delete(obj);
		//tr.commit();
		//声明式事务不需要手动给关闭Session
		//s.close();
	}

	public Object getById(Class clazz, Serializable id) {
		// TODO Auto-generated method stub
		Session s=getSession();
		Object obj=s.get(clazz, id);
		//声明式事务不需要手动给关闭Session
		//s.close();
		return obj;
	}

	public List queryByHql(String hql, List paramList,int firResult,int maxResults) {
		// TODO Auto-generated method stub
		Session s=getSession();
		Query q=s.createQuery(hql);
		for(int i=0;i<paramList.size();i++){
			Object paramObj=paramList.get(i);
			if(paramObj instanceof Date){
				q.setTimestamp(i, (Date)paramObj);
			}else{
				q.setParameter(i, paramObj);
			}
		}
		//如果查询起始位置>=0并且最大查询记录数>0，则进行分页
		if(firResult>=0&&maxResults>0){
			q.setFirstResult(firResult);
			q.setMaxResults(maxResults);
		}
		
		List queryList=q.list();
		//声明式事务不需要手动给关闭Session
		//s.close();
		return queryList;
	}

	public Object queryUniqueByHql(String hql, List paramList) {
		// TODO Auto-generated method stub
		Session s=getSession();
		Query q=s.createQuery(hql);
		for(int i=0;i<paramList.size();i++){
			Object paramObj=paramList.get(i);
			if(paramObj instanceof Date){
				q.setTimestamp(i, (Date)paramObj);
			}else{
				q.setParameter(i, paramObj);
			}
		}
		
		Object result=q.uniqueResult();
		//声明式事务不需要手动给关闭Session
		s.close();
		return result;
	}
}
